package com.skyline.wo.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.skyline.base.service.CommentService;
import com.skyline.base.type.CommentType;
import com.skyline.common.bean.Page;
import com.skyline.feed.service.PersonalFeedService;
import com.skyline.feed.type.FeedType;
import com.skyline.wo.dao.ShortMessageDao;
import com.skyline.wo.model.ShortMessage;
import com.skyline.wo.service.ShortMessageService;

@Service("shortMessageService")
public class ShortMessageServiceImpl implements ShortMessageService {

	@Autowired
	private ShortMessageDao shortMessageDao;

	@Autowired
	private CommentService commentService;

	@Autowired
	private PersonalFeedService personalFeedService;

	@Override
	public long addShortMessage(long ownerId, String ownerNickname, String ownerPortrait, String text) {
		// shortMessageDao.queryShortMessage(id)
		long newMessageId = shortMessageDao.insertShortMessage(ownerId, ownerNickname, ownerPortrait, text, null, 0, null);
		if (newMessageId != 0) {
			personalFeedService.addFeed(newMessageId, "短消息", text, ownerId, ownerNickname, ownerPortrait, ownerId, FeedType.SHORTMESSAGE);
		}
		return newMessageId;
	}

	@Override
	public long forwardShortMessage(long id, long ownerId, String ownerNickname, String ownerPortrait, String comment) {
		ShortMessage forwardedMessage = shortMessageDao.queryShortMessage(id);
		if (forwardedMessage == null) {
			return 0;
		} else {
			// String source="";
			long forwardId = shortMessageDao.insertShortMessage(ownerId, ownerNickname, ownerPortrait, forwardedMessage.getText(), comment,
					forwardedMessage.getOwnerId(), forwardedMessage.getOwnerNickname());

			if (forwardId != 0) {
				String text = forwardedMessage.getText();
				if (text.length() > 10) {
					text = text.substring(0, 10);
				}
				
				commentService.commentResource(ownerId, ownerNickname, ownerPortrait, comment, (long) 0, forwardId, text,
						CommentType.SHORTMESSAGE, forwardedMessage.getOwnerId());
				String content="";
				//if(forwardedMessage.getSourceOwnerId()!=0){
					String arch ="<a href='../user/"+forwardedMessage.getSourceOwnerId()+".html'>"+forwardedMessage.getOwnerNickname()+"</a>";
					content=comment+"&nbsp;转自"+arch+"<br>";
					content+="<div class='shortMessageContent'>"+arch+":"+forwardedMessage.getText()+"</div>";
					//content=
			//	}
				personalFeedService.addFeed(forwardId, "短消息", content, ownerId, ownerNickname, ownerPortrait, ownerId, FeedType.SHORTMESSAGE);
			}
			return forwardId;
		}
	}

	@Override
	public ShortMessage getShortMessageById(long id) {
		return shortMessageDao.queryShortMessage(id);
	}

	@Override
	public List<ShortMessage> getShortMessageByOwnerId(long ownerId,Page page) {
		return shortMessageDao.queryShortMessageByOwnerId(ownerId,page);
	}

	@Override
	public void deleteShortMessage(long id, long actionerId) {
		shortMessageDao.deleteShortMessage(id, actionerId);
	}

}
